module.exports = {
    template: `
        <div class="container w-xxl w-auto-xs">
          <a href class="navbar-brand w-full block m-t">UMS - 分销管理系统</a>
          <div class="m-b-lg">
            <div class="wrapper text-center">
              <strong>苏州开心盒子</strong>
            </div>
            <form name="form" class="form-validation">
              <div class="list-group list-group-sm">
                <div class="list-group-item">                                               
                  <input type="text" placeholder="Username" name="name" class="form-control no-border"    
                        v-model="userform.data.user_account.value"            
                        v-input-validate="userform.element.user_account"
                    required>
                </div>
                <div class="list-group-item">
                   <input type="password" placeholder="Password" class="form-control no-border" 
                    v-model="userform.data.user_password.value"          
                    v-input-validate="userform.element.user_password"   
                    required>
                </div>
              </div>
              <button type="button" class="btn btn-lg btn-info btn-block" 
              @click="login" :disabled="userform.checkError()">登录</button>
            </form>
          </div>
          <div class="text-center">
            <p>
              <small class="text-muted" v-html="footerPage"></small>
            </p>
           </div>         
        </div>    
    `,
    data() {
        var formFields = window.AUI.Utils.defineFormRules( {
            "user_account": {
                validate: {
                    presence: true
                }
            },
            "user_password": {
                validate: {
                    presence: true
                }
            }
        });

        var ret = {
            headerPage: window.rootcontroller.headerPage,
            footerPage: window.rootcontroller.footerPage,
            authError: "",
            app: window.rootcontroller.app,
        };

        ret.userform = window.Validator.genForm(formFields, _.keys(formFields));

        window.s = ret.userform;

        return ret;
    },
    mounted() {
        var self = this;
        window.Validator.genFormValidate(self, "userform");

        setTimeout(function() {
            if ($(".modal-backdrop").length > 0) {
                $(".modal-backdrop").remove();
            }
            self.userform.validateAll();
        }, 0);
    },
    methods: {
        login() {
            var self = this;
            var data = self.userform.getData();

            rootcontroller.services.requestService._post("tokens", data, {})
                .then(function (response) {
                rootcontroller.tokenManager.init(response.data.token);


                rootcontroller.setOldAppCookie(response, rootcontroller.tokenManager, data.user_account);

                var to = rootcontroller.constatnts.DEFAULTPAGE;

                if (Lscache.get(rootcontroller.constatnts.LASTROUTE)) {
                    to = Lscache.get(rootcontroller.constatnts.LASTROUTE).path;
                }

                rootcontroller.getUserInfo(response.data.token).then(function (response) {
                    Lscache.set(rootcontroller.constatnts.USERINFO, response.data);
                    window.aui.interceptors = {};

                    if (self.$route.query.callback_path) {
                        window.location.href = "http://" + self.$route.query.callback_path + "/#" + self.$route.query.callback_hash;
                    } else {
                        self.$router.push({
                            path: to
                        });
                    }

                }).catch(function (e) {
                    console.log('-------------------');
                    console.log(e);
                    console.log('-------------------');
                    console.error("用户信息错误");
                });
            }).catch(function (e) {
                self.authError = e.response.data.message;
            });
        }
    }
};

